<?php

//分类管理
class CategoryController extends Controller
{
	//分类列表
	public function actionList()
	{
		/*
		$dataTree=array(
		array(
			'text'=>'Grampa', //must using 'text' key to show the text
			'children'=>array(//using 'children' key to indicate there are children
				array(
					'text'=>'Father',
					'children'=>array(
						array('text'=>'me <a href="">删除</a>'),
						array('text'=>'big sis'),
						array('text'=>'little brother'),
					)
				),
				array(
					'text'=>'Uncle',
					'children'=>array(
						array('text'=>'Ben'),
						array('text'=>'Sally'),
					)
				),
				array(
					'text'=>'Aunt',
				)
			)
		)
	);*/
		$dataTree = Category::categoryTree(0);
		$this->render('list', array('dataTree'=>$dataTree));
	}
	
	//修改分类
	public function actionEdit()
	{
		$categoryModel = Category::model()->findByPk($_GET['category_id']);
		if (isset($_POST['Category']))
		{
			$categoryModel->attributes = $_POST['Category'];
			if($categoryModel->save())
			{
				$this->success('',Yii::app()->createUrl('admin/category/list'));
			}else{
				$this->error($categoryModel->getErrors());
			}
		}
		$this->render('edit',array('model'=>$categoryModel));
	}

	//新增分类
	public function actionAdd()
	{
		$categoryModel = new Category;
		if (isset($_POST['Category']))
		{
			$categoryModel->attributes = $_POST['Category'];
			if($categoryModel->save())
			{
				$this->success('',Yii::app()->createUrl('admin/category/list'));
			}else{
				$this->error($categoryModel->getErrors());
			}
		}
		$categoryModel->setAttribute('category_position', '1');
		$this->render('add',array('model'=>$categoryModel));
	}
	
	//删除分类
	public function actionDelete()
	{
		if(Category::model()->deleteByPk($_GET['category_id']))	$this->redirect(array('list'));
	}
}